package com.ahwei.service.impl;

import com.ahwei.context.AppContext;
import com.ahwei.dao.user.BrowseDao;
import com.ahwei.dao.user.UserDao;
import com.ahwei.dao.video.VideoDao;
import com.ahwei.dao.video.VideoDetailDao;
import com.ahwei.pojo.Browse;
import com.ahwei.pojo.User;
import com.ahwei.pojo.Video;
import com.ahwei.pojo.VideoDetail;
import com.ahwei.pojo.enums.Result;
import com.ahwei.pojo.model.PageResult;
import com.ahwei.service.VideoService;
import com.ahwei.util.MyBatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author：ahwei163@qq.com
 *
 * @date: 202*-03-16 16:57:13
 * @description: 这是一个Java文件！
 */
public class VideoServiceImpl implements VideoService {

    @Override
    public Result addVideo(Video video) {
        SqlSession sqlSession = MyBatisUtils.getSqlSession(true);
        VideoDao videoDao = sqlSession.getMapper(VideoDao.class);
        int addVideo = videoDao.add(video);
        //设置自增长编号
        video.getVideoDetail().setVid(video.getVid());
        VideoDetailDao videoDetailDao = sqlSession.getMapper(VideoDetailDao.class);
        int addVideoDetail = videoDetailDao.add(video.getVideoDetail());
        sqlSession.close();
        return addVideo > 0 && addVideoDetail > 0 ? Result.ADD_SUCCESS : Result.ADD_FAIL;
    }

    @Override
    public List<Video> findByType(long type) {
        SqlSession sqlSession = MyBatisUtils.getSqlSession(true);
        VideoDao videoDao = sqlSession.getMapper(VideoDao.class);

        List<Video> videoList = videoDao.findByType(type);
        sqlSession.close();
        return videoList;
    }

    @Override
    public List<Video> findColumnOrderBy(String column) {
        SqlSession sqlSession = MyBatisUtils.getSqlSession(true);
        VideoDao videoDao = sqlSession.getMapper(VideoDao.class);

        List<Video> columnOrderBy = videoDao.findColumnOrderBy(column);
        sqlSession.close();
        return columnOrderBy;
    }

    @Override
    public PageResult<Video> findAll(String keyword, String sort, String column,
                                     Integer pageNo, String account, String status) {
        SqlSession sqlSession = MyBatisUtils.getSqlSession(true);
        VideoDao videoDao = sqlSession.getMapper(VideoDao.class);
        //sql参数
        Map<String, Object> map = new HashMap<>();
        map.put("keyword", keyword);
        map.put("issuer", account);
        if (StringUtils.hasLength(status) && !"null".equals(status)) {
            map.put("status", status);
        }
        map.put("column", column);
        map.put("sort", sort);
        if (pageNo != null) {
            //每页条数乘以当前页数减一
            int getData = AppContext.PAGE_COUNT * (pageNo - 1);
            //limit ?,?
            map.put("getData", getData);
            map.put("pageCount", AppContext.PAGE_COUNT);
        }

        PageResult<Video> pageAble = new PageResult<>();
        //获取集合
        List<Video> all = videoDao.findAll(map);
        for (Video video : all) {
            // 设置中文状态
            video.setStatusName(String.valueOf(AppContext.videoStatus.get(String.valueOf(video.getStatus()))));
        }
        pageAble.setPageNo(pageNo);
        pageAble.setData(all);
        //设置最大页数
        pageAble.setMaxPageNo(videoDao.count(map));
        sqlSession.close();
        return pageAble;
    }


    @Override
    public Video findByVid(long vid, long uid) {
        SqlSession sqlSession = MyBatisUtils.getSqlSession(true);
        VideoDao videoDao = sqlSession.getMapper(VideoDao.class);
        Video video = videoDao.findByVid(vid);
        if (video != null) {
            // 判断视频是否是vip尊享
            if (String.valueOf(video.getStatus() ).equals(String.valueOf(AppContext.videoStatus.get("VIP尊享")))) {
                UserDao userDao = sqlSession.getMapper(UserDao.class);
                // 判断用户角色是否是普通用户
                User userByUid = userDao.findUserByUid(uid);
                if (userByUid.getRoleId() == 1) {
                    throw new RuntimeException("该视频为VIP尊享, 请开通VIP");
                }
            }
            VideoDetail videoDetail = video.getVideoDetail();
            //视频浏览量加一
            videoDetail.setBrowseCount(videoDetail.getBrowseCount() + 1);
            updateVideoDetail(videoDetail);
            BrowseDao browseDao = sqlSession.getMapper(BrowseDao.class);
            Browse browse = new Browse(uid, vid, new Date(), video);
            Browse exists = browseDao.isExists(vid,uid);

            if (exists == null) {
                browseDao.add(browse);
            }
        }
        sqlSession.close();
        return video;
    }

    @Override
    public int updateVideo(Video video) {
        SqlSession sqlSession = MyBatisUtils.getSqlSession(true);
        VideoDao videoDao = sqlSession.getMapper(VideoDao.class);
        int update = videoDao.update(video);
        sqlSession.close();
        return update;
    }

    @Override
    public Result updateVideoDetail(VideoDetail videoDetail) {
        SqlSession sqlSession = MyBatisUtils.getSqlSession(true);
        VideoDetailDao videoDetailDao = sqlSession.getMapper(VideoDetailDao.class);

        int result = videoDetailDao.updateVideoDetail(videoDetail);
        sqlSession.close();
        return result > 0 ? Result.UPDATE_SUCCESS : Result.UPDATE_FAIL;
    }

    @Override
    public List<Map<String, Object>> getVideoTypeList() {
        SqlSession sqlSession = MyBatisUtils.getSqlSession(true);
        VideoDao videoDao = sqlSession.getMapper(VideoDao.class);
        List<Map<String, Object>> videoTypeList = videoDao.getVideoTypeList();
        sqlSession.close();
        return videoTypeList;
    }

    @Override
    public Boolean addVideoType(String typeName) {
        SqlSession sqlSession = MyBatisUtils.getSqlSession(true);
        VideoDao videoDao = sqlSession.getMapper(VideoDao.class);
        int result = videoDao.addVideoType(typeName);
        sqlSession.close();
        return result > 0;
    }

    @Override
    public Boolean editVideoType(String typeName, String typeId) {
        SqlSession sqlSession = MyBatisUtils.getSqlSession(true);
        VideoDao videoDao = sqlSession.getMapper(VideoDao.class);
        int result = videoDao.editVideoType(typeName, typeId);
        sqlSession.close();
        return result > 0;
    }
}
